package com.dinghui.ebookbag.module.small;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dinghui.ebookbag.R;
import com.dinghui.ebookbag.module.small.fragment.ApplicationFragment;
import com.dinghui.ebookbag.module.small.fragment.BasicFragment;
import com.dinghui.ebookbag.module.small.fragment.DictionaryFragment;
import com.dinghui.ebookbag.module.small.fragment.ExpandFragment;
import com.dinghui.ebookbag.module.small.fragment.HomeFragment;
import com.dinghui.ebookbag.module.small.fragment.ToolFragment;

public class PrimarySchoolActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    RadioGroup ps_bottom_tabs;
    RadioButton ps_rb_1;
    FragmentManager fragmentManager;//管理fragment的类


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary_school);
        initView();
    }

    private void initView() {
        ps_bottom_tabs= (RadioGroup) findViewById(R.id.ps_bottom_tabs);
        ps_rb_1= (RadioButton) findViewById(R.id.ps_rb_1);
        //初始化FragmentManager
        fragmentManager=getSupportFragmentManager();
        //设置默认选中
        ps_rb_1.setChecked(true);
        ps_bottom_tabs.setOnCheckedChangeListener(this);
        //切换不同的fragment
        changeFragment(new HomeFragment(),false);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.ps_rb_1:
                changeFragment(new HomeFragment(),true);
                break;
            case R.id.ps_rb_2:
                changeFragment(new BasicFragment(),true);
                break;
            case R.id.ps_rb_3:
                changeFragment(new ExpandFragment(),true);
                break;
            case R.id.ps_rb_4:
                changeFragment(new DictionaryFragment(),true);
                break;
            case R.id.ps_rb_5:
                changeFragment(new ApplicationFragment(),true);
                break;
            case R.id.ps_rb_6:
                changeFragment(new ToolFragment(),true);
                break;
        }
    }

    //切换不同的fragment
    public void changeFragment(Fragment fragment, boolean isInit) {
        //开启事务
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.ps_content,fragment);
        if (!isInit) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
}